﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBProvider.Providers;
using DBProvider;

namespace HealthCommunity
{
	public partial class AskQuestion : System.Web.UI.Page
	{
		private IQuestionProvider _QuestionProvider;
		private List<DrugStore> lstDrugStore;

		private Question _CurrentQuestion;

		public int CurrentQuestionID
		{
			get
			{
				var id = CommonHelper.QueryStringInt("questionid");

				if (id <= 0)
				{
					if (ViewState["questionid"] != null)
					{
						id = (int)ViewState["questionid"];
					}
				}

				return id;
			}
			set
			{
				ViewState["questionid"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindQuestionCategory();
				BindDrugStore();
			}
		}

		protected void btnRemove_Click(object sender, EventArgs e)
		{
			if (lstMemberSelected.SelectedItem != null)
			{
				lstMemberSelected.Items.Remove(lstMemberSelected.SelectedItem);
			}
		}

		protected void btnAdd_Click(object sender, EventArgs e)
		{
			if (lstMember.SelectedItem != null)
			{
				var item = new System.Web.UI.WebControls.ListItem(lstMember.SelectedItem.Text, lstMember.SelectedItem.Value);

				if (!lstMemberSelected.Items.Contains(item))
				{
					lstMemberSelected.Items.Add(item);
				}
			}
		}

		protected void ddlDrugStore_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindMember();
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			var selectedQuestionCat = ddlQuestionCategory.SelectedValue;

			var theQuestionCatID = int.Parse(selectedQuestionCat);

			var rewardScores = 0;

			int.TryParse(txtRewardScores.Text.Trim(), out rewardScores);

			var user = SiteContext.Current.CurrentUser;

			if (_QuestionProvider == null)
			{
				_QuestionProvider = new QuestionProvider();
			}

			if (CurrentQuestionID <= 0)
			{
				var theNewQuestion = new Question() { QuestionAddTime = DateTime.Now, QuestionCategoryID = theQuestionCatID, QuestionContent = txtQuestion.Text.Trim(), QuestionState = (int)QuestionState.UnResolved, RewardScores = rewardScores, DrugStoreID = user.DrugStoreID.GetValueOrDefault(0) };

				theNewQuestion.IsPublic = chbIsPublic.Checked ? 1 : 0;

				if (user != null)
				{
					theNewQuestion.QuestionUserID = user.UserID;
				}

				var msg = string.Empty;
				_CurrentQuestion = _QuestionProvider.CreateQuestion(theNewQuestion, out msg);
				if (_CurrentQuestion != null)
				{
					this.CurrentQuestionID = _CurrentQuestion.QuestionID;

					if (!chbIsPublic.Checked)
					{
						List<int> lstSelectedDrugStoreIDs = new List<int>();
						foreach (System.Web.UI.WebControls.ListItem item in chblstDrugStore.Items)
						{
							if (item.Selected)
							{
								lstSelectedDrugStoreIDs.Add(int.Parse(item.Value));
							}
						}

						List<RLT_QuestionAndShareTo> lstNewQuestionAndShareTo = new List<RLT_QuestionAndShareTo>();

						if (lstSelectedDrugStoreIDs.Count > 0)
						{
							foreach (var id in lstSelectedDrugStoreIDs)
							{
								var rltQuestionAndShareTo = new RLT_QuestionAndShareTo() { QuestionID = CurrentQuestionID, ShareToDrugStoreID = id };
								lstNewQuestionAndShareTo.Add(rltQuestionAndShareTo);
							}
						}

						if (lstMemberSelected.Items.Count > 0)
						{
							foreach (System.Web.UI.WebControls.ListItem item in lstMemberSelected.Items)
							{
								var memberID = int.Parse(item.Value);
								var rltQuestionAndShareTo = new RLT_QuestionAndShareTo() { QuestionID = CurrentQuestionID, ShareToMemberID = memberID };
								lstNewQuestionAndShareTo.Add(rltQuestionAndShareTo);
							}
						}

						if (lstNewQuestionAndShareTo.Count > 0)
						{
							_QuestionProvider.CreateQuestionAndShareTo(lstNewQuestionAndShareTo);
						}
					}

					ltrMsg.Text = "问题提交成功";
				}
				else
				{
					ltrMsg.Text = msg;
				}
			}
			else
			{
				_CurrentQuestion = _QuestionProvider.GetQuestion(CurrentQuestionID);
				_CurrentQuestion.IsPublic = chbIsPublic.Checked ? 1 : 0;
				if (_CurrentQuestion != null)
				{
					if (_QuestionProvider.UpdateQuestion(_CurrentQuestion))
					{
						if (!chbIsPublic.Checked)
						{
							List<int> lstSelectedDrugStoreIDs = new List<int>();
							foreach (System.Web.UI.WebControls.ListItem item in chblstDrugStore.Items)
							{
								if (item.Selected)
								{
									lstSelectedDrugStoreIDs.Add(int.Parse(item.Value));
								}
							}

							List<RLT_QuestionAndShareTo> lstQuestionAndShareTo = new List<RLT_QuestionAndShareTo>();

							if (lstSelectedDrugStoreIDs.Count > 0)
							{
								foreach (var id in lstSelectedDrugStoreIDs)
								{
									var rltQuestionAndShareTo = new RLT_QuestionAndShareTo() { QuestionID = CurrentQuestionID, ShareToDrugStoreID = id };
									lstQuestionAndShareTo.Add(rltQuestionAndShareTo);
								}
							}

							if (lstMemberSelected.Items.Count > 0)
							{
								foreach (System.Web.UI.WebControls.ListItem item in lstMemberSelected.Items)
								{
									var memberID = int.Parse(item.Value);
									var rltQuestionAndShareTo = new RLT_QuestionAndShareTo() { QuestionID = CurrentQuestionID, ShareToMemberID = memberID };
									lstQuestionAndShareTo.Add(rltQuestionAndShareTo);
								}
							}

							_QuestionProvider.SaveQuestionAndShareTo(CurrentQuestionID, lstQuestionAndShareTo);
						}

						ltrMsg.Text = "问题保存成功";
					}
					else
					{
						ltrMsg.Text = "保存失败了";
					}
				}
			}
		}

		private void BindQuestionCategory()
		{
			if (_QuestionProvider == null)
			{
				_QuestionProvider = new QuestionProvider();
			}

			ddlQuestionCategory.Items.Clear();
			ddlQuestionCategory.DataSource = _QuestionProvider.GetAllQuestionCategory();
			ddlQuestionCategory.DataBind();

			//ddlQuestionCategory.Items.Insert(0, new ListItem("请选择问题类型", "0"));
		}

		private void BindDrugStore()
		{
			chblstDrugStore.Items.Clear();
			chblstDrugStore.DataSource = GetDrugStore();
			chblstDrugStore.DataBind();

			ddlDrugStore.Items.Clear();
			ddlDrugStore.DataSource = GetDrugStore();
			ddlDrugStore.DataBind();

			BindMember();
		}

		private void BindMember()
		{
			if (!string.IsNullOrWhiteSpace(ddlDrugStore.SelectedValue))
			{
				var selectedDrugStoreID = int.Parse(ddlDrugStore.SelectedValue);

				var userProvider = new UserProvider();

				lstMember.DataSource = userProvider.GetUserByDrugStore(selectedDrugStoreID, true);
				lstMember.DataBind();
			}
		}

		private List<DrugStore> GetDrugStore()
		{
			List<DrugStore> result;

			if (this.lstDrugStore != null)
			{
				result = lstDrugStore;
			}
			else
			{
				var drugStoreProvider = new DrugStoreProvider();

				lstDrugStore = drugStoreProvider.GetAllDrugStore();

				result = lstDrugStore;
			}

			return result;
		}
	}
}